package com.qf.dao.impl;

import com.qf.dao.UserDao;
import com.qf.entity.User;
import com.qf.utils.Constants;
import com.qf.utils.DBUtil;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * @Author:YuhuaZhang
 * @Description 数据库访问实现类
 * @Date:Created in 10:53 2021/11/4
 * @Modified By:
 */
public class UserDaoImpl implements UserDao {


    @Override
    public User selectUserByUname(String username) throws SQLException {

        //1.创建一个QueryRunner对象
        QueryRunner queryRunner = new QueryRunner();

        //2.执行sql语句
        String sql = "select u_id as uid,u_name as username,u_password as upassword" +
                ",u_sex as usex,u_status as ustatus,u_code as code,u_email as email" +
                ",u_role as urole from user where u_name=?";

        User user = queryRunner.query(DBUtil.getConnection(), sql, new BeanHandler<>(User.class), username);

        return user;
    }

    @Override
    public int insertUser(User user) throws SQLException {
        //1.创建一个QueryRunner对象
        QueryRunner queryRunner = new QueryRunner();

        //2.执行sql语句插入数据
        String sql="insert into user(u_name,u_password,u_sex,u_status,u_code,u_email,u_role) value(?,?,?,?,?,?,?)";

        int rows = queryRunner.update(DBUtil.getConnection(), sql, user.getUsername(), user.getUpassword(),
                user.getUsex(), user.getUstatus(), user.getCode(), user.getEmail(), user.getUrole());

        return rows;
    }

    @Override
    public User selectUserByCode(String code) throws SQLException {
        //1.创建一个QueryRunner对象
        QueryRunner queryRunner = new QueryRunner();

        //2.执行sql语句
        String sql = "select u_id as uid,u_name as username,u_password as upassword" +
                ",u_sex as usex,u_status as ustatus,u_code as code,u_email as email" +
                ",u_role as urole from user where u_code=?";

        User user = queryRunner.query(DBUtil.getConnection(), sql, new BeanHandler<>(User.class), code);

        return user;

    }

    @Override
    public int updateStatusByUid(int uid) throws SQLException {
        //1.创建一个QueryRunner对象
        QueryRunner queryRunner = new QueryRunner();

        //2.执行sql语句
        String sql="update user set u_status=? where u_id=?";

        int row = queryRunner.update(DBUtil.getConnection(), sql, Constants.USER_ACTIVE, uid);
        return row;
    }
}
